// pages/used/used.js
const App = getApp();
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    closeBool:true,
    //查看是否登录
    // isLogin:wx.getStorageSync('markId'),
    bannerList:[],
    tabScrollTop: 0,
    tabFixed: false,
    //商品状态筛选
    selectSptypes:'全部',
    //价格筛选
    selectPrice:'价格升序',
    //详细筛选是否隐藏
    selectDetail:false,
    //商品状态筛选列表
    sptypesList:[
      { id: '0,1,3,4,5', name: '现货', checked: false },
      { id: '2', name: '预售', checked: false },
    ],
    //商品属性列表
    maintypesList:[
      { id: 0, name: '拼装模型', checked: false },
      { id: 2, name: '成品不可动手办', checked: false },
      { id: 15, name: '成品可动人偶模型', checked: false },
      { id: 3, name: '景品', checked: false },
      { id: 4, name: 'Q版人偶', checked: false },
      { id: 7, name: '毛绒', checked: false },
      { id: 5, name: '盒蛋', checked: false },
      { id: 6, name: '扭蛋', checked: false },
      { id: 8, name: '儿童玩具', checked: false },
      { id: 10, name: '立牌', checked: false },
      { id: 11, name: '其他周边产品', checked: false },
      { id: 12, name: '虚拟商品', checked: false },
      { id: 13, name: '其他类商品', checked: false },
    ],
    //是否加密
    filterList:[
      { id: 0, name: '全部商品', checked: false }
    ],
    //列表请求参数
    page:0,
    markid:wx.getStorageSync('markId'),
    minmoney:1,
    maxmoney:99999999,
    sort:'time',
    sptypes:'',
    key:'',
    maintypes:'',
    filter:1,
    hasMoreData:true,
    list:[],//列表数据,
    noContent:false//数据为空
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //调用参数
    this.saltedEggList()
    this.bannerList();
    var that = this;
    wx.getSystemInfo({
      success (res) {
        // console.log(res)
        var statusBarHeight2 = res.statusBarHeight + 45
        that.setData({
        statusBarHeight:res.statusBarHeight,
        statusBarHeight2:statusBarHeight2,
       })
      }
    })
  },
    onSlideChangeEnd: function (e) {
    var that = this;
    if (e.detail.source == "autoplay" || e.detail.source == "touch"){
      that.setData({
        index: e.detail.current
      })
    }
  },
  //banner图
  bannerList:function () {
    utils.requestUrl({
      url:'/niu/apiv3/dic_keyinfo.php',
      params: {
        type:3
      }
    }).then((res) =>{
      let data = res.data.data;
      // console.log(data)
      var picsArr = [];
      var urlArr = [];
      data.forEach((item) => {
        picsArr = item.value1.split(';')
        urlArr = item.value2.split(';')
      })
      picsArr.pop();
      urlArr.pop();
    //  console.log(picsArr)
      this.setData({
        picsArr:picsArr,
        urlArr:urlArr
      })
    })
  },
  goToBannerDetail:function (e) {
    var index = e.currentTarget.dataset.index;
    if(index == 'jsbanner'){
      wx.navigateTo({
        url: '/pages/used/jsDetail/jsDetail'
      })
    }else{
      var url = this.data.urlArr[index];
      var urlStr = url.slice(15)
      wx.navigateTo({
        url: '/pages/used/bannerDetail/bannerDetail?url='+urlStr,
      })
    }
  },
  //商品状态筛选
  changeSelectSptypes:function (){
    var that = this;
    wx.showActionSheet({
      itemList: ['全部', '现货', '预售'],
      success (res) {
        var selectSptypes;
        var sptypes;//商品状态
        if (res.tapIndex == 0) {
          selectSptypes = '全部';
          sptypes = ''
        }else if (res.tapIndex == 1){
          selectSptypes = '现货'
          sptypes = '0,1,3,4,5'
        }else if (res.tapIndex == 2){
          selectSptypes = '预售'
          sptypes = 2
        }

        that.setData({
          page:0,
          minmoney:1,
          maxmoney:99999999,
          selectSptypes:selectSptypes,
          sptypes:sptypes,
          list:[]
        })
        that.saltedEggList();
      },
      fail (res) {
        // console.log(res.errMsg)
      }
    })
  },
  //价格筛选
  changeSelectPrice:function(){
    var that = this;
    wx.showActionSheet({
      itemList: ['价格升序', '价格降序'],
      success (res) {
        var selectPrice;
        var sort;//排序
        if (res.tapIndex == 0) {
          selectPrice = '价格升序';
          sort = 'money'
        }else if (res.tapIndex == 1){
          selectPrice = '价格降序'
          sort = 'moneydesc'
        }

        that.setData({
          page:0,
          minmoney:1,
          maxmoney:99999999,
          selectPrice:selectPrice,
          sort:sort,
          list:[]
        })
        that.saltedEggList();
      },
      fail (res) {
        // console.log(res.errMsg)
      }
    })
  },
  //最新排序
  changeSelectTime:function (){
    var that = this;
    that.setData({
      page:0,
      sort:'time',
      list:[]
    })
    that.saltedEggList();
  },
  //详细筛选
  changeSelectDetail:function () {
    var that = this;
    that.setData({
      page:0,
      selectDetail:true
    })
  },
  //商品类型筛选
  sptypesListSelect:function (e) {
    let index = e.currentTarget.dataset.index;
    let sptypesList = this.data.sptypesList;
    let newli = 'sptypesList[' + index + '].checked';
    this.setData({
      [newli]: !this.data.sptypesList[index].checked
    })
  },
  //加密筛选
  filterList:function(e){
    let index = e.currentTarget.dataset.index;
    let filterList = this.data.filterList;
    let newli = 'filterList[' + index + '].checked';
    this.setData({
      [newli]: !this.data.filterList[index].checked
    })
  },
  //商品属性筛选
  maintypesListSelect:function (e) {
    let index = e.currentTarget.dataset.index;
    let maintypesList = this.data.maintypesList;
    let newli = 'maintypesList[' + index + '].checked';
    this.setData({
      [newli]: !this.data.maintypesList[index].checked
    })
  },
  //确定筛选
  confirmSelect:function (e){
    let sptypesList = this.data.sptypesList;
    let maintypesList = this.data.maintypesList;
    let filterList = this.data.filterList;
    let minmoney = this.data.minmoney;
    let maxmoney = this.data.maxmoney;
    // console.log(maxmoney)
    if (minmoney > maxmoney) {
      wx.showToast({
        title: '最低价不能大于最高价',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let sptypesListId = [];
    let maintypesListId = [];
    for (let i = 0; i < sptypesList.length; i++) {
      if (sptypesList[i].checked) {
        sptypesListId.push(sptypesList[i].id);
      }
    }
    for (let i = 0; i < maintypesList.length; i++) {
      if (maintypesList[i].checked) {
        maintypesListId.push(maintypesList[i].id);
      }
    }
    if (filterList[0].checked) {
      this.setData({
        filter:0
      })
    }else{
      this.setData({
        filter:1
      })
    }
    let sptypes = sptypesListId.toString(',');
    let maintypes = maintypesListId.toString(',');
    this.setData({
      page:0,
      sptypes:sptypes,
      selectDetail:false,
      maintypes:maintypes,
      list:[]
    })
    this.saltedEggList();
  },
  //重置筛选
  resetSelect:function () {
    let sptypesList = this.data.sptypesList;
    let maintypesList = this.data.maintypesList;
    for (let i = 0; i < sptypesList.length; i++) {
      sptypesList[i].checked = false
    }
    for (let i = 0; i < maintypesList.length; i++) {
      maintypesList[i].checked = false
    }
    this.setData({
      //是否加密
      filterList:[
        { id: 0, name: '全部商品', checked: false }
      ],
      sptypesList:sptypesList,
      maintypesList:maintypesList,
      minmoney:1,
      maxmoney:99999999,
    })
    // this.saltedEggList();
  },
  //获取最低价
  getMinmoneyInput:function(e){//方法1
		this.setData({
      minmoney: e.detail.value
    })
  },
   //获取最高价
   getMaxmoneyInput:function(e){//方法1
    let maxMoney = (e.detail.value) * 100
		this.setData({
      maxmoney: maxMoney
    })
  },
  // // 点击键盘上的搜索
  // bindconfirm:function(e){
  //   var discountName=e.detail.value['search - input'] ?e.detail.value['search - input'] : e.detail.value 
  //   this.setData({
  //     page:0,
  //     key: discountName,
  //     list:[]
  //   })
  //   this.saltedEggList();
  // },
   // 键盘离焦的搜索
   bindblur:function(e){
    var discountName=e.detail.value['search - input'] ?e.detail.value['search - input'] : e.detail.value 
    this.setData({
      page:0,
      key: discountName,
      filter:0,
      list:[]
    })
    this.saltedEggList();
  },
  bindinput:function(e){
    var val = e.detail.value;
    var closeBool;
    if (val == '') {
      closeBool = true
    }else{
      closeBool = false
    }
    this.setData({
      closeBool:closeBool
    })
  },
  closeSearch:function (){
    this.setData({
      closeBool:true,
      page:0,
      key: '',
      list:[]
    })
    this.saltedEggList();
  },
  //详细筛选隐藏
  hidden:function () {
    this.setData({
      selectDetail:false
    })
  },
  //列表数据
  saltedEggList:function (){
    var that = this;
    let page = that.data.page;
    let markid = that.data.markid;
    let minmoney = that.data.minmoney;
    let maxmoney = that.data.maxmoney;
    let sort = that.data.sort;
    let sptypes = that.data.sptypes;
    let key = that.data.key;
    let maintypes = that.data.maintypes;
    let filter = that.data.filter;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: App.globalData.url+'/niu/apiv3/xiandanv3_getrmblist.php',
      method: 'GET',
      data: {
        page:page,
        markid:markid,
        minmoney:minmoney,
        maxmoney:maxmoney,
        sort:sort,
        sptypes:sptypes,
        key:key,
        maintypes:maintypes,
        filter:filter,
      },
      header: {
        'PUA': 'wxsapp'
      },
      success: (res) => {
        console.log(res)
        wx.hideLoading()
        var contentListTem = that.data.list;
        if (res.data.datacount == 0 || res.data.datacount == null) {
          that.setData({
            noContent:true,
            hasMoreData:true
          })
        }else{
          that.setData({
            noContent:false
          })
        }
        if (res.data.datacount > 0) {
          var contentList = res.data.data
          

          if (contentList.length < res.data.pagesize) {
            that.setData({
              list:contentListTem.concat(contentList),
              hasMoreData: false
            })
          }else{
            that.setData({
              list: contentListTem.concat(contentList),
              hasMoreData: true,
              page: page + 1
          })
          }
        }
      }
    })
  },
  //去往详情
  goToDetail:function (e) {
    let id = e.currentTarget.dataset.id;
    let toysid = e.currentTarget.dataset.toysid;
    let toyscount = e.currentTarget.dataset.toyscount
    if (toyscount > '1') {
      wx.navigateTo({
        url: '/pages/used/usedToysList/usedToysList?id='+toysid,
      })
    }else{
      wx.navigateTo({
        url: '/pages/used/usedDetail/usedDetail?id='+id,
      })
    }
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    // console.log(e)
    // console.log(this.data.tabScrollTop)
    if (e.scrollTop > this.data.tabScrollTop) {
      this.setData({
        tabFixed: true
      })
    }else{
      this.setData({
        tabFixed: false
      })
    }
  },
  gotowxapp:function () {
    wx.navigateToMiniProgram({
      appId: 'wx71fd8865ba7ff0f5',
      path: '',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    var query = wx.createSelectorQuery()
    query.select('#tabScrollTop').boundingClientRect(function (res) {
      // console.log(res)
      that.setData({
        tabScrollTop: res.height
      })
    }).exec()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      bannerList:[],
      page:0,
      list:[],
    })
    this.saltedEggList()
    this.bannerList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.saltedEggList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})